package com.situ.meetingRoom.controller;

import com.situ.meetingRoom.entity.Dept;
import com.situ.meetingRoom.entity.Result;
import com.situ.meetingRoom.entity.User;
import com.situ.meetingRoom.service.DeptService;
import com.situ.meetingRoom.service.impl.DeptServiceImpl;
import com.situ.meetingRoom.util.BaseServlet;
import com.situ.meetingRoom.util.PageInfo;
import com.situ.meetingRoom.vo.UserVO;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet(name = "DeptServlet", value = "/dept")
public class DeptServlet extends BaseServlet {
    private DeptService deptService = new DeptServiceImpl();

    public void search(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //分页
        //获取参数
        String sPage = request.getParameter("page");
        String sLimit = request.getParameter("limit");

        //1-获取参数
        String name = request.getParameter("name");
        String sStatus = request.getParameter("status");

        Dept dept = new Dept();
        dept.setName(name);
        if (sStatus != null && sStatus.length()>0) {
            dept.setStatus(Integer.parseInt(sStatus));
        }

        Integer page = null, limit = null;
        try {
            page = Integer.parseInt(sPage);
            limit = Integer.parseInt(sLimit);
        } catch (Exception e) {
        }
        //判断是否分页
        if (page != null) {
            //分页
            PageInfo pageInfo = deptService.getForPage(page, limit ,dept);
            response.setContentType("application/json; charset=UTf-8");
            response.getWriter().append(Result.ok(pageInfo).toJson());
        } else {
            List<Dept> depts = deptService.getAll(dept);
            //返回，以JSON格式返回
            response.setContentType("application/json; charset=UTf-8");
            response.getWriter().append(Result.ok(depts).toJson());
        }
    }

    public void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1-获取参数
        String name = request.getParameter("name");
        //2-参数校验

        //3-调用service
        Dept dept = new Dept();
        dept.setName(name);
        try {
            deptService.add(dept);
            response.setContentType("application/json; charset=UTF-8");
            response.getWriter().append(Result.ok().toJson());
        } catch (Exception e) {
            e.printStackTrace();
            response.setContentType("application/json; charset=UTF-8");
            response.getWriter().append(Result.error(e.getMessage()).toJson());
        }
    }

    public void remove(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1-获取参数
        String sId = request.getParameter("id");
        try {
            Integer id = Integer.parseInt(sId);

            //2-调用service
            deptService.remove(id);

            //3-返回数据
            response.setContentType("application/json; charset=UTF-8");
            response.getWriter().append(Result.ok().toJson());
        } catch (Exception e) {
            e.printStackTrace();
            response.setContentType("application/json; charset=UTF-8");
            response.getWriter().append(Result.error(e.getMessage()).toJson());
        }
    }

    public void edit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1-获取参数
        String sId = request.getParameter("id");
        String name = request.getParameter("name");
        Integer id = Integer.parseInt(sId);
        //2-参数校验
        //3-调用service
        Dept dept = new Dept();
        dept.setId(id);
        dept.setName(name);
        try {
            deptService.edit(dept);
            response.setContentType("application/json; charset=UTF-8");
            response.getWriter().append(Result.ok().toJson());
        } catch (Exception e) {
            e.printStackTrace();
            response.setContentType("application/json; charset=UTF-8");
            response.getWriter().append(Result.error(e.getMessage()).toJson());
        }
    }

    public void getById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1-获取参数
        String sId = request.getParameter("id");

        Integer id = Integer.parseInt(sId);

        //2-调用service
        Dept dept = deptService.getById(id);

        response.setContentType("application/json; charset=UTF-8");
        response.getWriter().append(Result.ok(dept).toJson());


    }
}
